﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRM
{
    public partial class ServiceInfo : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录...");
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("2_3") < 0)
            {
                Common.MessageBox.Show(Page, "你没有编辑的权限...", "Body.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            RpServiceUser.DataSource =
                BLL.ServiceUser.GetAllServiceUserInfo("");
            RpServiceUser.DataBind();
        }

        protected void RpPowerUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="Del")   //通过CommandName判断当前单击的是哪个按钮
            {
                if (!string.IsNullOrEmpty
                    (e.CommandArgument.ToString())) //通过CommandArgument得到用户Id
                {
                    if (BLL.ServiceUser.DelServiceUserByID
                        (e.CommandArgument.ToString())) //删除当前用户
                    {
                        //提示删除成功
                        Common.MessageBox.Show(Page, "人员信息成功删除..",
                            "ServiceInfo.aspx", "suess");
                    }
                    else
                    {
                        //否则提示删除失败
                        Common.MessageBox.Show(Page, 
                            "人员信息删除失败，请重试或联系技术人员..", "error");
                    }
                }
            }
            if (e.CommandName=="Pass")    //如果选中命令为重置用户密码
            {
                if (!string.IsNullOrEmpty 
                    (e.CommandArgument.ToString()))  //得到当前的要重置密码的用户Id
                {
                    string newPass = Common.GetMes.GetRandomNum(6); //得到随机密码
                    if (BLL.ServiceUser.UpdateUserPass
                        (e.CommandArgument.ToString(), newPass))   //更新密码
                    {
                        //显示成功消息并重定向页面
                        Common.MessageBox.Show
                            (Page, "人员密码重置成功，新密码[ "+newPass+" ]..",
                            "ServiceInfo.aspx", "suess");
                    }
                    else
                    {
                        //显示失败消息
                        Common.MessageBox.Show(Page, 
                            "人员密码重置失败，请重试或联系技术人员..", "error");
                    }
                }
            }
        }
    }
}
